12.5 Initializing the Jump Table
Based on the descriptions provided across the preceding sections, it can be observed that during the source compilation process, the compiler toolchain only initializes the static keys and emits the __jump_table section. It arranges the branch-related segments at their respective code paths but does not execute instruction hot-patching on the placeholder sites based on the evaluation of the static keys. The task of performing instruction patching is handled by an initialization routine, a requirement executed by the function jump_label_init(). Upon completing the memory node and architectural zone initialization workflows, the start_kernel() process invokes jump_label_init(). This routine leverages each individual jump entry structure situated between __start___jump_table and __stop___jump_table, along with the runtime value of the static key referenced by that structure, to generate either a NOP instruction or a branch instruction. If the generated instruction type resolves to a NOP, the instruction location referenced by the code field of that jump entry is overwritten with a hardware NOP instruction. The function jump_label_init() is defined inside the source file linux/kernel/jump_label.c, and its definition is as follows:
void __init jump_label_init(void)
{
struct jump_entry *iter_start = __start___jump_table;
struct jump_entry *iter_stop = __stop___jump_table;
struct static_key *key = NULL;
struct jump_entry *iter;
if (static_key_initialized)
return;
cpus_read_lock();
jump_label_lock();
jump_label_sort_entries(iter_start, iter_stop);
for (iter = iter_start; iter < iter_stop; iter++) {
struct static_key *iterk;
if (jump_label_type(iter) == JUMP_LABEL_NOP)
arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
if (init_section_contains((void *)jump_entry_code(iter), 1))
jump_entry_set_init(iter);
iterk = jump_entry_key(iter);
if (iterk == key)
continue;
key = iterk;
static_key_set_entries(key, iter);
}
static_key_initialized = true;
jump_label_unlock();
cpus_read_unlock();
}
This routine first utilizes the function jump_label_sort_entries() to sort the jump entries located between __start___jump_table and __stop___jump_table in ascending order based on the magnitude of their code field values. Following this sorting pass, it iterates through all available jump entries. The function jump_label_type() is utilized to evaluate the truth table outlined in Table 12-2, executing an XOR bitwise operation to determine the exact patch instruction type. The definition of this function is as follows:
static enum jump_label_type jump_label_type(struct jump_entry *entry)
{
struct static_key *key = jump_entry_key(entry);
bool enabled = static_key_enabled(key);
bool branch = jump_entry_is_branch(entry);
return enabled ^ branch;
}
If the value of the static key's enabled field is greater than zero, static_key_enabled() returns a value of 1; otherwise, it returns a value of 0. jump_entry_is_branch(entry) returns the value of the least significant bit (b0) of the key field within the target jump entry.
The function arch_jump_label_transform_static() is responsible for updating the instruction target referenced by the entry's code field to the format specified by the type parameter. If the designated target type is JUMP_LABEL_NOP, this routine writes a hardware NOP instruction into the address designated by code. If the instruction type resolves to a branch target, the function leverages the values of the entry's code field and target field to generate a valid branch instruction, and subsequently writes the generated instruction into the address referenced by the code field.
The code sequence init_section_contains() verifies whether the instruction address referenced by the entry's code field resides within the initialization code segment (bounded between __init_begin and __init_end). If the target address resides within the initialization segment, it invokes the function jump_entry_set_init() to set bit b1 (the second bit) of the key field inside the jump entry, denoting that the initialization pass for this jump entry has completed. Following the initialization of this jump entry, the routine leverages static_key_set_entries() to map the entries field within the matching static key to store the base address of this jump entry.
After traversing all jump entries within the static jump label array, the program sets the global variable static_key_initialized to 1, indicating that the initialization phase for the jump table has completed successfully.
If a subsequent operational flow requires toggling a NOP placeholder into a concrete branch instruction, this transformation can be executed at runtime by invoking arch_jump_label_transform_static(iter, JUMP_LABEL_JMP).